package com.haole.ecommercepay.withdraw.service.impl;

import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.haole.ecommercepay.common.HttpUtils;
import com.haole.ecommercepay.withdraw.entity.QueryWithdrawParam;
import com.haole.ecommercepay.withdraw.entity.WithdrawParam;
import com.haole.ecommercepay.withdraw.service.WithdrawService;
import com.haole.ecommercepay.withdraw.url.WithdrawUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 * @author haole
 */
@Service("withdrawService")
public class WithdrawServiceImpl implements WithdrawService {

    private static final Logger logger = LoggerFactory.getLogger(WithdrawServiceImpl.class);

    /**
     * 账户余额提现
     * @param param
     * @return
     */
    @Override
    public JSONObject withdraw(WithdrawParam param) {
        String requestParam = JSON.toJSONString(param);
        logger.info("withdraw param:{}",requestParam);
        HttpResponse response = HttpUtils.v3HttpExecute("POST", WithdrawUrl.WITHDRAW,
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),requestParam,param.getMchPrivateKeyPath(),null);
        logger.info("withdraw result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 通过微信支付提现单号查询提现状态
     * @param param
     * @return
     */
    @Override
    public JSONObject withdrawId(QueryWithdrawParam param) {
        logger.info("withdraw withdrawId param:{}",param);
        HttpResponse response = HttpUtils.v3HttpExecute("GET", String.format(WithdrawUrl.WITHDRAW_ID,param.getWithdraw_id(),param.getSub_mchid()),
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("withdraw withdrawId result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
    /**
     * 通过商户提现单号查询提现状态
     * @param param
     * @return
     */
    @Override
    public JSONObject outRequestNo(QueryWithdrawParam param) {
        logger.info("withdraw outRequestNo param:{}",param);
        HttpResponse response = HttpUtils.v3HttpExecute("GET", String.format(WithdrawUrl.OUT_REQUEST_NO,param.getOut_request_no(),param.getSub_mchid()),
                param.getAppName(),param.getMchId(),param.getMchSerialNo(),
                param.getWechatSerialNo(),"",param.getMchPrivateKeyPath(),null);
        logger.info("withdraw outRequestNo result:{}",response);
        return HttpUtils.httpResult(response,param.getWechatPubKeyPath());
    }
}
